package inits

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"socials/appconfig"
	"socials/golbal"
	"socials/modle"
)

var err error

func Mysql() {
	data := appconfig.ConfigData.Mysql
	dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local",
		data.User, data.Password, data.Host, data.Port, data.Data)
	// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
	//dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
	golbal.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	} else {
		fmt.Println("数据库连接成功")
	}
	err = golbal.DB.AutoMigrate(&modle.User{}, &modle.Contact{}, &modle.Chat{}, &modle.Moment{}, &modle.Program{})
	if err != nil {
		panic(err)
	} else {
		fmt.Println("数据表迁移成功")
	}
}
